package com.javadaemon.Tinderbox;

import com.javadaemon.Tinderbox.gui.ControlPanel;
import com.javadaemon.Tinderbox.gui.HotkeyListener;
import com.javadaemon.Tinderbox.model.Misc;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.KeyboardFocusManager;
import java.awt.Toolkit;
import java.awt.Window;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import com.javadaemon.Tinderbox.loader.GameAppletLoader;
import com.javadaemon.Tinderbox.loader.ParamaterParser;
import javax.swing.JOptionPane;

/**
 * This file is part of Tinderbox.
 *
 * Tinderbox is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ponix is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tinderbox.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Class Tinderbox, the client's main class.
 * @author Mads Horndrup
 */
public class Tinderbox {

    /** URL, grabs a URL from a regular page, giving a bit of extra insurance. */
    public static String URL;

    /** Image used as icon for the windows */
    public static Image image;

    /** Grabs screen size (for fullscreen) at startup. */
    private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    
    /** Boolean that stores whether or not the fullscreen mode is toggled. */
    private static boolean isFullScreen = false;

    /** Fullscreen cached window */
    public static Window window;

    /** The main applet panel, the rs applet is added to this, so it is not reloaded on fullscreen toggle. */
    public JPanel appletPanel = new JPanel();
    
    /** Simple, the mainFrame title. */
    private final String frameTitle = "Tinderbox v" + Constants.VERSION;
     
    /** The main content mainFrame, for holding the applet itself non fullscreen. */
    public static JFrame mainFrame;

    /** The control panel. */
    public static ControlPanel controlpanel;

    /** RS Properties storage. */
    public HashMap<String, String> props;

    /** The game applet loader instance. */
    private static GameAppletLoader loader;

    public static void main(String args[]) {
        new Tinderbox(args);
    }

    private Tinderbox(String args[]) {
        super();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            ParamaterParser parser = new ParamaterParser();
            URL = parser.parseURL();
            parser.readPage();
            props = parser.parseParamaters();
        } catch (IOException e) {
            JOptionPane.showMessageDialog(new JFrame(), "Failed to load Runescape. Try again.");
        }
        initializeGUI();
    }

    /*
     * Initializes the basic graphical user interface.
     */
    private void initializeGUI() {
        try {
            image = Toolkit.getDefaultToolkit().getImage(new URL("http://www.javadaemon.com/tinderbox/icon.png"));

            // Set up the mainFrame.
            mainFrame = new JFrame(frameTitle);
            mainFrame.setLayout(new BorderLayout());
            mainFrame.setIconImage(image);
            mainFrame.setResizable(true);
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            // Set up the applet.
            loader = new GameAppletLoader(this);
            appletPanel.setLayout(new BorderLayout());
            appletPanel.add(loader.getApplet());
            appletPanel.setPreferredSize(new Dimension(765, 503));

            // Put the GUI together.
            mainFrame.getContentPane().add(appletPanel);
            mainFrame.pack();
            mainFrame.setVisible(true);
            window = new Window(mainFrame);
            window.setBounds(0, 0, screenSize.width, screenSize.height);
            window.setFocusable(true);
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new HotkeyListener(this));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String parseClientLoader() {
        return URL.substring(0, URL.lastIndexOf("/")) + "/loader_gl.jar";
    }

    public static boolean isFullscreen() {
        return isFullScreen;
    }

    public void toggleFullscreen() {
        if (!isFullscreen()) {
            mainFrame.getContentPane().remove(appletPanel);
            window.add(appletPanel);
            window.setVisible(true);
            mainFrame.setTitle("Tinderbox - Fullscreen");
            isFullScreen = true;
        } else {
            window.remove(appletPanel);
            mainFrame.getContentPane().add(appletPanel);
            window.setVisible(false);
            mainFrame.pack();
            mainFrame.setTitle(frameTitle);
            isFullScreen = false;
        }
    }

    public static GameAppletLoader getLoader() {
        return loader;
    }

    public void initializeControlPanel() {
        controlpanel = new ControlPanel(this);
    }

    public static ControlPanel getControlPanel() {
        return controlpanel;
    }

    public void destroyControlPanel() {
        controlpanel = null;
    }

    public static Image getImage() {
        return image;
    }
}
